<?php 
	require_once("function/initialize.php");
	require_once("function/file.php");
	openConnection();
	if(isset($_REQUEST["logout"]))
		logout();
	if(!isLoggedIn())
		pageRedirect("login.php","");
		
	$sql_cart = "SELECT * FROM tbl_cart_stock";
	$result_cart = mysql_query($sql_cart);
	$row_cart=fetch_assoc($result_cart);
	//check if cart empty	
	
	if(isset($_GET['control']) && $_GET['control'] == "insert"){
		$stocknumber=$_POST['stocknumber'];
		$billnumber=$_POST['billnumber'];
		//select date n convert date
		$selected_date=$_POST['date'];
		$selected_date=strtotime($selected_date );
		$mysqldate = date( 'Y-m-d', $selected_date );
		$dateStock=$mysqldate;
		
		$supplier=$_POST['supplier'];
		if($supplier==""){
			$supplier="NaN";
		}
		$nameArray=$_POST['name'];
		$quantity=$_POST['quantity'];
		$brate=$_POST['brate'];
		$srate=$_POST['srate'];
		$avail=$_POST['avail'];
		$total=$_POST['total'];
		$payment=$_POST['payment'];
		$balance=$_POST['balance'];
		$subtotal=$_POST['subtotal'];		
		$mode=$_POST['mode'];
		//select date n convert date
		$selected_lastdate=$_POST['lastupdate'];
		$selected_lastdate=strtotime($selected_lastdate );
		$mysqllastdate = date( 'Y-m-d', $selected_lastdate );
		$lastupdate=$mysqllastdate;
		$userName=getUserType();
		$i=0;
		$j=1;
		foreach($nameArray as $name){
			$quantity=$_POST['quantity'][$i];
			$brate=$_POST['brate'][$i];
			$srate=$_POST['srate'][$i];
			$total=$_POST['total'][$i];
			
				//select qty to update
				$sql="SELECT qty FROM  tbl_stocks_available WHERE pd_name='$name'";
				$result=query($sql);
				$Newqty=fetch_assoc($result);
				$oldqty=$Newqty['qty'];
				$Newqty=$oldqty+$quantity;
				
				//update stock available
				$sql="UPDATE tbl_stocks_available SET qty='$Newqty' WHERE pd_name='$name'";
				$result=query($sql);
				
				//insert sell product
				$sql="INSERT INTO  tbl_stocks (stock_id,billnumber,pd_date,sup_name,pd_name,qty,buying_price,selling_price,total,paid
				     ,balance,amount,mode,user_name,pd_last_update,opening_stock,closing_stock,type,count)
				      VALUES('$stocknumber','$billnumber','$dateStock','$supplier','$name','$quantity','$brate','$srate','$total'
				     ,'$payment','$balance','$subtotal','$mode','$userName','$lastupdate','$oldqty','$Newqty','entry','$j')";
				$result=query($sql);
				
				//update stock available
				$sql="UPDATE tbl_products SET buying_price='$brate',selling_price ='$srate' WHERE pd_name='$name'";
				$result=query($sql);
				
			$i+=1;
			$j++;	
		}//end foreach	
		
		if($result){
					$sql_del="DELETE FROM tbl_cart_stock";
					$result_del=query($sql_del);
					if($result_del){
						pageRedirect("index.php?page=add_stock&action=add_stock_new&","Successful...!");
					}
				}
	}
	
	if(isset($_GET['control']) && $_GET['control'] == "update"){	
		$stocknumber=$_POST['stocknumber'];
		$billnumber=$_POST['billnumber'];
		//select date n convert date
		$selected_date=$_POST['date'];
		$selected_date=strtotime($selected_date );
		$mysqldate = date( 'Y-m-d', $selected_date );
		$dateStock=$mysqldate;
		
		$supplier=$_POST['supplier'];
		if($supplier==""){
			$supplier="NaN";
		}
		$nameArray=$_POST['name'];
		$quantity=$_POST['quantity'];
		$brate=$_POST['brate'];
		$srate=$_POST['srate'];
		$avail=$_POST['avail'];
		$total=$_POST['total'];
		$payment=$_POST['payment'];
		$balance=$_POST['balance'];
		$subtotal=$_POST['subtotal'];		
		$mode=$_POST['mode'];
		//select date n convert date
		$selected_lastdate=$_POST['lastupdate'];
		$selected_lastdate=strtotime($selected_lastdate );
		$mysqllastdate = date( 'Y-m-d', $selected_lastdate );
		$lastupdate=$mysqllastdate;
		$userName=getUserType();
		$i=0;
		$j=1;
		foreach($nameArray as $name){
			$quantity=$_POST['quantity'][$i];
			$brate=$_POST['brate'][$i];
			$srate=$_POST['srate'][$i];
			$total=$_POST['total'][$i];
			$sysid=$_POST['sysid'][$i];
			
				//select old qty to update
				$sql="SELECT qty FROM  tbl_stocks_available WHERE pd_name='$name'";
				$result=query($sql);
				$row=fetch_assoc($result);
				$totalQty=$row['qty'];
				
				//select  qty from tbl_stock
				$sql="SELECT qty FROM tbl_stocks WHERE stock_id='$stocknumber' AND count=$j";
				$result=query($sql);
				$row=fetch_assoc($result);
				$oldqty=$row['qty'];
				//calculate ot update qty in stock available
				$newQty=($totalQty+$quantity)-$oldqty;

				
				//update stock available
				$sql="UPDATE tbl_stocks_available SET qty='$newQty' WHERE pd_name='$name'";
				$result=query($sql);
				
				//update stock entry
				$sql="UPDATE tbl_stocks SET pd_date='$dateStock',sup_name='$supplier',pd_name='$name',qty='$quantity',
				buying_price='$brate',selling_price='$srate',total='$total',paid='$payment',balance='$balance',amount='$subtotal'
				,mode='$mode',user_name='$userName',pd_last_update='$lastupdate',opening_stock='$oldqty',closing_stock='$newQty'
				WHERE stock_id='$stocknumber' AND count='$j' ";
				$result=query($sql);
				
				//update stock available
				$sql="UPDATE tbl_products SET buying_price='$brate',selling_price ='$srate' WHERE pd_name='$name'";
				$result=query($sql);
				
			$i++;
			$j++;	
		}//end foreach	
		
		pageRedirect("index.php?page=add_stock&action=add_stock&","Updated successful...!");
	}
	
	if(isset($_GET['action']) && $_GET['action'] == "delete"){
		$sql = "DELETE FROM tbl_customers
				WHERE id = $_GET[id]";
		mysql_query($sql);		
		header("Location:index.php?page=customer_list&action=customer_list");
	}
?>